﻿using UnityEngine;
using System;
using System.Reflection;
using UnityEditor;

namespace CWM.Skinn
{
    public static partial class SkinnEditorEx
    {
        public static partial class UnityIDs
        {
            public const string DefaultPose = "WriteDefaultPose";
            public const string SceneView = "UnityEditor.SceneView";
            public const string Inspector = "UnityEditor.Inspector";

        }

        public static void RepaintSceneView()
        {
            if (Application.isPlaying) return;
            SceneView.RepaintAll();
        }

        public static EditorWindow GetSceneViewWindow
        {
            get
            {
                var inspectorWindow = SceneView.focusedWindow;
                if (inspectorWindow) return inspectorWindow;
                var assembly = typeof(UnityEditor.EditorWindow).Assembly;
                var window = assembly.GetType(UnityIDs.SceneView);
                if (window == null) { return null; }
                inspectorWindow = EditorWindow.GetWindow(window);
                return inspectorWindow;
            }
        }

        public static EditorWindow GetInspectorWindow
        {
            get
            {
                var assembly = typeof(UnityEditor.EditorWindow).Assembly;
                var window = assembly.GetType(UnityIDs.Inspector);
                if (window == null) { return null; }
                var inspectorWindow = EditorWindow.GetWindow(window);
                return inspectorWindow;
            }
        }

        public static void LogType(Type type, string methodHint = null)
        {
            var methodTypes = typeof(Animator).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
            string availbleMethods = "";
            foreach (var item in methodTypes)
            {
                var itemNameID = item.Name.ToLower();
                if (!string.IsNullOrEmpty(methodHint) && !itemNameID.Contains(methodHint.ToLower())) continue;
                string allParameters = "Parameters :";
                var parameters = item.GetParameters();
                for (int i = 0; i < parameters.Length; i++)
                {
                    allParameters += " " + parameters[i].Name.ToString();
                    string parameterType = parameters[i].ParameterType.Name.ToString();
                    string isOptional = parameters[i].IsOptional.ToString();
                    string isOut = parameters[i].IsOut.ToString();

                    if (!string.IsNullOrEmpty(parameterType)) allParameters += ", parameterType " + parameterType;
                    if (!string.IsNullOrEmpty(isOptional)) allParameters += ", isOptional " + isOptional;
                    if (!string.IsNullOrEmpty(isOut)) isOut += ", isOut " + isOut;
                }
                availbleMethods += parameters.Length < 1 ? item.Name : string.Format("{0}, {1}", item.Name, allParameters);
                availbleMethods += Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(methodHint) && string.IsNullOrEmpty(availbleMethods))
            {
                Debug.LogWarning("method hint '" + methodHint + "'  failed.");
                LogType(type, null);
                return;
            }
            Debug.Log(availbleMethods);
        }
    }
}
